package mall

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"main.go/global"
	"main.go/model/common/request"
	"main.go/model/common/response"
	"main.go/model/mall"
	mallReq "main.go/model/mall/request"
	"main.go/utils"
)

type OrderApi struct{}

func (a *OrderApi) Create(c *gin.Context) {
	var r mall.MallOrder
	err := c.ShouldBindJSON(&r)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	orderReturn, err := orderService.Create(&r)
	if err != nil {
		global.EVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithDetailed(orderReturn, "创建失败", c)
		return
	}
	response.OkWithDetailed(orderReturn, "创建失败", c)
}
func (a *OrderApi) Delete(c *gin.Context) {
	var reqId request.GetById
	err := c.ShouldBindJSON(&reqId)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(reqId, utils.IdVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = orderService.Delete(reqId.ID)
	if err != nil {
		global.EVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
		return
	}
	response.OkWithMessage("删除成功", c)

}
func (a *OrderApi) Update(c *gin.Context) {
	var data mall.MallOrder
	err := c.ShouldBindJSON(&data)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(data, utils.IdVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	_, err = orderService.Update(&data)
	if err != nil {
		global.EVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
		return
	}

	response.Ok(c)
}
func (a *OrderApi) List(c *gin.Context) {
	var pageInfo mallReq.MallOrderListReq
	err := c.ShouldBindJSON(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(pageInfo, utils.PageInfoVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := orderService.List(&pageInfo)
	if err != nil {
		global.EVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}
